<?php
class class_ABMPlanes
{
	private $_db; 
	function __construct(){
		$this->config();
	}
	
	function method_getLista($params, $error){
		$this->_db->setQuery("
			SELECT 
				planes.*, 
				obras_sociales.descripcion AS obra_social
			FROM planes
			LEFT JOIN obras_sociales ON obras_sociales.cod_obra_social = planes.cod_obra_social
		");
		$this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$result = array();
		$regs = $this->_db->loadResult();
		$row = array();
    	FOREACH ($regs as $reg){
			array_push($row, $reg->cod_plan);
			array_push($row, $reg->obra_social);
			array_push($row, $reg->descripcion);
			array_push($row, $reg->detalles);
			array_push($result, $row);
			$row = array();
		}
		return $result;
    }
    
	function method_btnBorrar($params, $error){
		$error->SetError(JsonRpcError_ParameterMismatch, "NO PUEDE ELIMINAR PLANES, LLAMAR A PROGRAMADORES");
		return $error;
        if (count($params) != 1){ $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        $this->_db->setQuery("DELETE FROM planes WHERE cod_plan= '" . $params[0] . "'");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    
	function method_btnModificar($params, $error){
        if (count($params) != 1){ $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
		$this->_db->setQuery("SELECT * FROM planes WHERE cod_plan = '" . $params[0] . "' LIMIT 1");
		$this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$reg = $this->_db->loadResult();
		return $reg[0];
    }
    
	function method_opAlta($params, $error) {
		/* $error->SetError(JsonRpcError_ParameterMismatch, "NO PUEDE CARGAR PLANES, LLAMAR A PROGRAMADORES");
		return $error;
		 */
        if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        $this->_db->setQuery("
        	INSERT INTO planes (
        		cod_plan, 
        		cod_obra_social, 
        		descripcion,
        		detalles
        		) 
        		VALUES(
        		NULL, 
        		'" . $params[0]->cod_obra_social . "',
        		'" . $params[0]->descripcion . "',
				'" . $params[0]->detalles . "'        		
        		)
        ");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    
	function method_opModificacion($params, $error){
		if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
	    $this->_db->setQuery("
	    	UPDATE 
	    		planes SET cod_obra_social = '" . $params[0]->cod_obra_social. "', 
	    		detalles = '" . $params[0]->detalles. "'
	    		WHERE cod_plan= '" . $params[0]->cod_plan . "' 
	    		LIMIT 1
	    ");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    function method_getObrasSociales($params, $error){
		$this->_db->setQuery("SELECT cod_obra_social AS value, descripcion AS label FROM obras_sociales");
		$this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$regs = $this->_db->loadResult();
		$result->values = $regs;
		$result->nullValue = true;
		return $result;
    }        
	private function config(){
		require_once("conexion.php");
		require_once("DataBase.php");
		$this->_db = new DataBase($SERVIDOR,$USUARIO,$PASSWORD,$BASE);
		mysql_query("SET NAMES utf8");
	}
}
?>